var models = require('./connectmysql');//数据库链接信息
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('./sqlfun');//sql语句

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();

// 增加用户接口
const addUser=require('./childinterface/resface');
router.post('/addUser',addUser);
//登录接口
const login=require('./childinterface/loginface');
router.post('/login',login);
//用户修改接口
const upUser=require('./childinterface/updataUserMsg');
router.post('/upUser',upUser);
//食材接口
const baseMes=require('./childinterface/basefoodface');
router.post('/selectbasemes',baseMes);
//论坛接口
const bbsMes=require('./childinterface/foodbbsMes');
router.post('/selectfoodbbs',bbsMes);
//论坛文章图片缓存接口
const Uploadtemporary=require('./childinterface/Uploadtemporary');
router.post('/Uploadtemporary',Uploadtemporary);
//论坛文章图片缓存接口
const Uploadimage=require('./childinterface/Uploadimage');
router.post('/Uploadimage',Uploadimage);
//论坛子级接口
const bbschildMes=require('./childinterface/foodbbsctxMsg');
router.post('/selectfoodctxMsg',bbschildMes);
//论坛增加评论接口
const bbscomMes=require('./childinterface/foodbbscommentMsg');
router.post('/addbbscomMsg',bbscomMes);
//论坛删除评论接口
const deletcomMes=require('./childinterface/deletarticlMsg');
router.post('/deletbbscomMsg',deletcomMes);

router.post('/selectshop', (req, res) => {
    var sql = $sql.datames.selectshop;
    console.log("sql",sql);
    conn.query(sql, function(err, data) {
      if(err){
        var result={
          "status":"500",
          "message":"服务器错误"
        }
        console.log(result.status);
        return res.jsonp(result);
      }
      else {
        var result={
          "status":"200",
          "message":"success",
          data:data
        }
        console.log(result.status);
        return res.jsonp(result);
      }
    })
  }
);
router.post('/selectshopbyid', (req, res) => {
  var params = req.body;
  console.log("params",params);
  var sql = $sql.datames.selectshopbyid(params.id);
  console.log("sql",sql);
  conn.query(sql, function(err, data) {
    if(err){
      var result={
        "status":"500",
        "message":"查询错误"
      }
      console.log(result.status);
      return res.jsonp(result);
    }
    else {
      var result={
        "status":"200",
        "message":"success",
        data:data
      }
      console.log(result.status);
      return res.jsonp(result);
    }
  })
});
router.post('/addMsg', (req, res) => {
  var params = req.body;
  console.log("params",params);
  var sql = $sql.datames.addMsg(params.userId, params.shopId, params.msg);
  console.log("sql",sql);

  let _conn = conn;
  conn.query(sql, function(err, data) {
    if(err){
      var result={
        "status":"500",
        "message":"查询错误",
        "err": err
      }
      console.log("err", err.sqlMessage)
      return res.jsonp(result);
    }
    else {
      _conn.query($sql.datames.selectuserbyid(params.userId), function(err, data) { if(!err) {
        //console.log(data[0].name)
        var result={
          "status":"200",
          "message":"success",
          data: {
            userName: data[0].name,
            msg: params.msg
          }
        }
        return res.jsonp(result);
      }})
    }
  })
});
router.post('/selectcook', (req, res) => {
    var sql = $sql.datames.selectcook;
    var params = req.body;
    console.log("sql",sql);
    console.log("params",params);
    conn.query(sql, function(err, data) {
      if(err){
        var result={
          "status":"500",
          "message":"服务器错误"
        }
        console.log(res.jsonp(result));
        return res.jsonp(result);
      }
      else {
        var result={
          "status":"200",
          "message":"success",
          data:data
        }
        console.log(res.jsonp(result));
      }
    })
  }
);
//今日菜单详情
router.post('/foodMsg',(req,res)=>{
    var sql =$sql.todayfood.selectfoodMsg;
    var name = req.body.foodname;
    conn.query(sql,[name],function(err,result){
      if(err){
        console.log(err);
      }
     if(result){
        console.log(result);
        res.send(result);
      }
    })
})
router.post('/tijiao', (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');//允许跨域
  let body = JSON.parse(req.body);//转换josn对象为普通对象
  let tijiao = body.tijiao;     //前端发过来的参数
  let yuju = 'select * from user_msg where username = ?';
  connection.query(yuju,[tijiao] ,(err,result) =>{
    if (err) return console.log(err);
    console.log(result);
    if (result.tijiao == '987') {
      res.send('提交成功');
      res.end();
    }
  })

});


module.exports = router;
